const adminmember_collection = db.collection("memberinfo");
(function() {
	var _con = function(info) {
		var self = this;
		if(info){
			self.info = info;
		}
	}

	module.exports = _con;

	_con.prototype.list = co.wrap(function*(data) {
        var self = this;
       	if(typeof self.info != 'object' || !self.info._id){
       		return printError('您不是管理员');
       	}
       	var num = 10;
       	var page = data.page?parseInt(data.page):1;
       	page = Math.max(1,page)-1;
       	var param = {
            skip: num * page,
            limit: num,
            sort: {
                createtime: -1
            },
            query:{}
        };
        var status = parseInt(data.status);
        if(status.length>0){
        	param.query.status = status;
        }
        return yield mongo.find(adminmember_collection,param);
    });

    _con.prototype.updateStatus = co.wrap(function*(data){
		var self = this;
		/*if(typeof self.info != 'object' || !self.info._id){
       		return printError('您不是管理员');
       	}*/
       	if(!data._id){
       		return printError('申请记录不存在！');
       	}
       	var memberinfo = yield mongo.findOne(adminmember_collection,{query:{_id:data._id}});
       	if(!memberinfo){
       		return printError('申请记录不存在1！');
       	}
       	if(memberinfo.status!=1){
       		return printError('已经审核过了！');
       	}
       	data.status = parseInt(data.status);
		var arr_status = [0,2];
		var bool = false;
		for(var i=0;i<arr_status.length;i++){
			if(data.status==arr_status[i]){
				bool = true;
				break;
			}
		}
		if(!bool){
			return printError('状态错误！');
		}
		var param = {query:{_id:data._id},data:{status:data.status}};
		var returninfo = yield mongo.updateOne(adminmember_collection,param);
		if(data.status==2){
			var params = {
				_id:memberinfo.uid,
				data:{
					realname:memberinfo.realname,
					gender:memberinfo.gender,
					idcard:memberinfo.idcard,
					job:memberinfo.job,
					type:memberinfo.type
				}
			};
			var user_collection = db.collection("user");
			returninfo = yield mongo.updateOneByObjectId(user_collection,params);
			return returninfo;
		}
		return returninfo;
    });

     _con.prototype.detail = co.wrap(function*(_id) {
        var self = this;
        var params = {query:{_id:_id}};
        return yield mongo.findOne(adminmember_collection,params);
    });
}());
